package net.xju.dao;

import net.xju.JDBCUtils;
import net.xju.common.utils.JSONUtil;
import net.xju.common.utils.JdbcUtilsV1;
import net.xju.entity.Work;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;

public class WorkDao {
    public static void insert(Work work) {
        String sql="insert into work (workname,type,careInfo,username) VALUES(?,?,?,?)";
        JdbcUtilsV1.executeUpdate(sql,work.getWorkName(),work.getType(),work.getCareInfo(),work.getUsername());
    }

    public Work queryByUsername(String username) {
        String sql="select * from work where userName=?";
        return JdbcUtilsV1.executeQueryOne(JdbcUtilsV1.getConnection(),Work.class,sql,username);
    }

    public void deleteInFact(String username) {
        String sql="delete from work where userName=?";
        JdbcUtilsV1.executeUpdate(sql,username);
    }

    public boolean updateByUsername(Work work) {
        String sql="update work SET workname=?,type=?,careInfo=? WHERE username=?";
        int i=JdbcUtilsV1.executeUpdate(sql,work.getWorkName(),work.getType(),work.getCareInfo(),work.getUsername());
        return i>0?true:false;
    }

    public long countWork(String type) throws SQLException {
        String sql = "SELECT COUNT(*) FROM work WHERE type=?";
        Long cnt = new QueryRunner().query(JdbcUtilsV1.getConnection(), sql, new ScalarHandler<>(1), type);
        return cnt == null ? 0 : cnt;
    }
}
